package bb.lanxing.util.ui;

import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;

public class FlipBitmapDisplayer implements BitmapDisplayer {
    private static final String TAG = "FlipBitmapDisplayer";
    private final int durationMillis;
    private boolean enable;

    public FlipBitmapDisplayer(int i) {
        this.durationMillis = i;
    }

    public void enableAnimation(boolean z) {
        this.enable = z;
    }

    @Override
    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
        animate(imageAware, bitmap, this.durationMillis);
    }

    private void animate(ImageAware imageAware, Bitmap bitmap, int i) {
        if (this.enable) {
            imageAware.getWrappedView().clearAnimation();
            Rotate3DAnimation rotate3DAnimation = new Rotate3DAnimation(bitmap, imageAware);
            rotate3DAnimation.setDuration(i);
            rotate3DAnimation.setInterpolator(new LinearInterpolator());
            imageAware.getWrappedView().startAnimation(rotate3DAnimation);
            return;
        }
        imageAware.setImageBitmap(bitmap);
    }

    public class Rotate3DAnimation extends Animation {
        private Bitmap bitmap;
        private boolean hasChanged;
        private ImageAware imageAware;
        private Camera mCamera;
        private float mCenterX;
        private float mCenterY;

        private Rotate3DAnimation(Bitmap bitmap, ImageAware imageAware) {
            this.bitmap = bitmap;
            this.imageAware = imageAware;
        }

        @Override
        public void initialize(int i, int i2, int i3, int i4) {
            super.initialize(i, i2, i3, i4);
            this.mCamera = new Camera();
            this.mCenterX = i / 2;
            this.mCenterY = i2 / 2;
            this.hasChanged = false;
        }

        @Override
        protected void applyTransformation(float f, Transformation transformation) {
            super.applyTransformation(f, transformation);
            float f2 = 180.0f * f;
            Matrix matrix = transformation.getMatrix();
            boolean z = f > 0.5f;
            if (z && !this.hasChanged) {
                this.imageAware.setImageBitmap(this.bitmap);
                this.imageAware.getWrappedView().invalidate();
                this.hasChanged = true;
            }
            this.mCamera.save();
            this.mCamera.rotateY(f2);
            this.mCamera.getMatrix(matrix);
            this.mCamera.restore();
            if (z) {
                matrix.preScale(-1.0f, 1.0f);
            }
            matrix.preTranslate(-this.mCenterX, -this.mCenterY);
            matrix.postTranslate(this.mCenterX, this.mCenterY);
        }
    }
}
